<?php
/**
 * 消息管理控制器
 * User: wuhao
 * Date: 2017/4/17
 * Time: 15:15
 */
namespace app\backend\controller;

use app\backend\controller\traits\AuthManager;
use app\backend\model\MessageTemplate;
use app\backend\model\PayNotice;
use think\Controller;

class MessageController extends Controller
{
    use AuthManager;
    private $_memberId = null;
    private $_dtype    = null;
    private $_pdtype   = null;
    private $_pmid     = null;
    private $_allAuth  = [];
    public function __construct()
    {
        parent::__construct();
        $memberInfo = $this->getLoginInfo();
        $this->_memberId = $memberInfo['mid'];
        $this->_dtype    = $memberInfo['dtype'];
        $this->_pdtype   = $memberInfo['p_dtype'];
        $this->_pmid     = $memberInfo['p_mid'];
        $this->_allAuth  = $this->getPower($this->_memberId, $this->_dtype);
    }

    /**
     * 新增消息模板
     * @date   2017-04-17
     * @author wuhao
     *
     * @return json
     */
    public function add()
    {
        if (!in_array($this->_pdtype, [0])) {
            $this->apiReturn(400, [], '无权操作');
        }

        $title        = I('post.title', '', 'strval');              //消息标题
        $contractType = I('post.contract_type', 1, 'intval');       //合同类型 1=租赁合约  2=托管合约
        $eventType    = I('post.event_type', 1, 'intval');          //通知事件 1=催缴、2=到账、3=到期
        $messageType  = I('post.message_type', 1, 'intval');        //消息类型 1: 公告 2: 提醒 3：信息
        $member       = I('post.member', 1, 'intval');              //通知对象 1=付款方 2=收款方
        $sendType     = I('post.send_type', 1, 'intval');           //发送对象 1=系统  2=人工
        $notifyMethod = I('post.notify_method', 1, 'intval');       //通知方式 1=短信 2=邮件 3=系统弹窗
        $notifyDay    = I('post.notify_day', 5, 'intval');          //提前通知天数
        $content      = I('post.content');                          //显示消息的内容  1=账单周期 2=具体费用 3=合计金额 4=房源门牌号 5=缴费时间

        /*$title='test1';$contractType=1;$eventType=1;$messageType=2;$member=1;$sendType=1;$notifyMethod=1;$notifyDay=7;$content=[1,2,3];
        $content = json_encode($content);*/
        if (!$title) {
            $this->apiReturn(400, [], '消息标题不能为空');
        }
        if (!$contractType || !in_array($contractType, [1, 2])) {
            $this->apiReturn(400, [], '合同类型有误');
        }
        if (!$eventType || !in_array($eventType, [1, 2, 3])) {
            $this->apiReturn(400, [], '通知事件有误');
        }
        if (!$messageType || !in_array($messageType, [1, 2, 3])) {
            $this->apiReturn(400, [], '消息类型有误');
        }
        if (!$sendType || !in_array($sendType, [1, 2])) {
            $this->apiReturn(400, [], '发送对象有误');
        }
        if (!$member || !in_array($member, [1, 2])) {
            $this->apiReturn(400, [], '通知对象有误');
        }
        if (!$notifyMethod || !in_array($notifyMethod, [1, 2, 3])) {
            $this->apiReturn(400, [], '通知方式有误');
        }
        if ($contractType == 1) {
            if (!$notifyDay || !is_numeric($notifyDay) || $notifyDay <=0 ) {
                $this->apiReturn(400, [], '通知天数有误');
            }
        }

        if (!$content) {
            $this->apiReturn(400, [], '内容配置有误');
        }
        if (($eventType == 1 && $member == 2)) {
            $this->apiReturn(400, [], '催缴用户不能设置为收款方');
        }
        $contentTmp = json_decode($content, true);
        foreach ($contentTmp as $k => $c) {
            if (!in_array($c, [1,2,3,4,5])) {
                $this->apiReturn(400, [], '内容配置有误');
            }
            $contentTmp[$k] = (int) $c;
        }
        $content = json_encode($contentTmp);

        //检测模板是否已存在
        $messageModel = new MessageTemplate();
        $checkEvent   = $messageModel->checkEvent($contractType, $eventType, $member);
        //$checkMessage = $messageModel->checkMessage($contractType, $messageType, $member);
        if ($checkEvent) {
            $this->apiReturn(201, [], '消息模板已存在');
        }

        //添加模板
        $res = $messageModel->addTemplate($this->_pmid, $title, $contractType, $eventType, $messageType, $member, $sendType, $notifyMethod, $notifyDay, $content);
        if (!$res) {
            zft_log('message/add', $messageModel->getLastSql());
            $this->apiReturn(202, [], '添加出错');
        } else {
            $this->apiReturn(200, [], '添加成功');
        }
    }

    /**
     * 检查是否已存在这种类型的模板
     * @date   2017-04-17
     * @author wuhao
     *
     * @return json
     */
    public function check()
    {
        if (!in_array($this->_pdtype, [0])) {
            $this->apiReturn(400, [], '无权操作');
        }
        $contractType = I('post.contract_type', 1, 'intval');       //合同类型 1=租赁合约  2=托管合约
        $eventType    = I('post.event_type', 1, 'intval');          //通知事件 1=催缴、2=到账、3=到期
        $messageType  = I('post.message_type', 1, 'intval');        //消息类型 1: 公告 2: 提醒 3：信息
        $member       = I('post.member', 1, 'intval');              //通知对象 1=付款方 2=收款方
        if (!$contractType || !in_array($contractType, [1, 2])) {
            $this->apiReturn(400, [], '合同类型有误');
        }
        if (!$eventType || !in_array($eventType, [1, 2, 3])) {
            $this->apiReturn(400, [], '通知事件有误');
        }
        if (!$messageType || !in_array($messageType, [1, 2, 3])) {
            $this->apiReturn(400, [], '消息类型有误');
        }
        if (!$member || !in_array($member, [1, 2])) {
            $this->apiReturn(400, [], '通知对象有误');
        }

        $messageModel = new MessageTemplate();
        $checkEvent = $messageModel->checkEvent($contractType, $eventType, $member);
        $checkMessage = $messageModel->checkMessage($contractType, $messageType, $member);
        if ($checkEvent || $checkMessage) {
            $this->apiReturn(201, [], '消息模板已存在');
        } else {
            $this->apiReturn(200, [], '');
        }
    }

    /**
     * 查询已有模板
     * @date   2017-04-18
     * @author wuhao
     *
     * @return json
     */
    public function search()
    {
        if (!in_array($this->_pdtype, [0])) {
            $this->apiReturn(400, [], '无权操作');
        }

        $title        = I('post.title', '', 'strval');              //消息标题
        $eventType    = I('post.event_type', 0, 'intval');          //通知事件 0=全部 1=催缴、2=到账、3=到期
        $messageType  = I('post.message_type', 0, 'intval');        //消息类型 0=全部 1: 公告 2: 提醒 3：信息
        $page         = I('post.page', 1, 'intval');                //页数
        $pageSize     = I('post.page_size', 10, 'intval');          //每页个数
        $messageModel = new MessageTemplate();
        $res = $messageModel->searchTemplate($title, $eventType, $messageType, $page, $pageSize);
        if ($res) {
            $this->apiReturn(200, $res, '');
        } else {
            $this->apiReturn(201, [], '暂无数据');
        }
    }

    /**
     * 删除模板
     * @date   2017-04-18
     * @author wuhao
     *
     * @return json
     */
    public function delete()
    {
        if (!in_array($this->_pdtype, [0])) {
            $this->apiReturn(400, [], '无权操作');
        }
        $id = I('post.id', '', 'intval');  //模板id
        if (!$id || !is_numeric($id)) {
            $this->apiReturn(400, [], '参数错误');
        }
        $messageModel = new MessageTemplate();
        if ($messageModel->deleteTemplate($id)) {
            $this->apiReturn(200, [], '删除成功');
        } else {
            zft_log('message/delete', $messageModel->getLastSql());
            $this->apiReturn(201, [], '删除失败');
        }
    }

    /**
     * 编辑模板
     * @date   2017-04-18
     * @author wuhao
     *
     * @return json
     */
    public function edit()
    {
        if (!in_array($this->_pdtype, [0])) {
            $this->apiReturn(400, [], '无权操作');
        }
        $id           = I('post.id', '', 'intval');                 //模板id
        $title        = I('post.title', '', 'strval');              //标题
        $sendType     = I('post.send_type', 1, 'intval');           //发送对象 1=系统  2=人工
        $notifyMethod = I('post.notify_method', 1, 'intval');       //通知方式 1=短信 2=邮件 3=系统弹窗
        $notifyDay    = I('post.notify_day', 5, 'intval');          //提前通知天数
        $content      = I('post.content');                          //显示消息的内容  1=账单周期 2=具体费用 3=合计金额 4=房源门牌号 5=缴费时间
       /* $id=2;$title='vvv';$sendType=2;$notifyMethod=1;$notifyDay=15;$content=[1,2];
        $content=json_encode($content);*/
        if (!$id || !is_numeric($id) || !$title || !$sendType || !in_array($sendType, [1, 2]) || !$notifyMethod || !in_array($notifyMethod, [1, 2, 3])
            || !$notifyDay || $notifyDay<=0 || !$content) {
            $this->apiReturn(400, [], '参数错误');
        }
        $messageModel = new MessageTemplate();
        $detailInfo   = $messageModel->getTemplateById($id);
        if (!$detailInfo) {
            $this->apiReturn(400, [], '找不到模板信息');
        }
        if (!$title) {
            $this->apiReturn(400, [], '消息标题不能为空');
        }
        if (!$sendType || !in_array($sendType, [1, 2])) {
            $this->apiReturn(400, [], '发送对象有误');
        }
        if (!$notifyMethod || !in_array($notifyMethod, [1, 2, 3])) {
            $this->apiReturn(400, [], '通知方式有误');
        }
        if (!$notifyDay || !is_numeric($notifyDay) || $notifyDay <=0 ) {
            $this->apiReturn(400, [], '通知天数有误');
        }
        if (!$content) {
            $this->apiReturn(400, [], '内容配置有误');
        }
        $contentTmp = json_decode($content, true);
        foreach ($contentTmp as $k => $c) {
            $contentTmp[$k] = (int) $c;
            if (!in_array($c, [1,2,3,4,5])) {
                $this->apiReturn(400, [], '内容配置有误');
            }
        }
        $content = json_encode($contentTmp);

        if ($content == $detailInfo['content'] && $notifyDay == $detailInfo['notify_day'] && $title == $detailInfo['title']
            && $notifyMethod == $detailInfo['notify_method'] && $sendType == $detailInfo['send_type'] ) {
            $this->apiReturn(400, [], '模板信息未作修改');
        }
        //编辑模板
        $res = $messageModel->editTemplate($id, $title, $sendType, $notifyMethod, $notifyDay, $content);
        if ($res) {
            $this->apiReturn(200, [], '编辑成功');
        } else {
            zft_log('message/edit', $messageModel->getLastSql());
            $this->apiReturn(201, [], '编辑失败');
        }
    }

    /**
     * 获取某个模板详细信息
     * @date   2017-04-18
     * @author wuhao
     *
     * @return json
     */
    public function detail()
    {
        if (!in_array($this->_pdtype, [0])) {
            $this->apiReturn(400, [], '无权操作');
        }
        $id = I('post.id', '', 'intval');  //模板id
        if (!$id || !is_numeric($id)) {
            $this->apiReturn(400, [], '参数错误');
        }
        $messageModel = new MessageTemplate();
        $detailInfo   = $messageModel->getTemplateById($id);
        if (!$detailInfo) {
            $this->apiReturn(400, [], '找不到模板信息');
        } else {
            $detailInfo['content'] = json_decode($detailInfo['content'], true);
            $this->apiReturn(200, $detailInfo, '');
        }
    }
}